//
//  ViewModel.m
//  DebugDemo
//
//  Created by XFB on 2018/11/16.
//  Copyright © 2018年 ZhouCheng. All rights reserved.
//

#import "ViewModel.h"
#import "Model.h"

@implementation ViewModel

- (NSMutableArray *)dataArray
{
    if (_dataArray == nil) {
        _dataArray = @[].mutableCopy;
    }
    return _dataArray;
}

- (instancetype)init
{
    if (self = [super init]) {
        /// 利用KVO
        [self addObserver:self forKeyPath:@"dataArray" options:NSKeyValueObservingOptionNew context:nil];
    }
    return self;
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context
{
    NSLog(@"%@", change[NSKeyValueChangeNewKey]);
    self.successBlock(change[NSKeyValueChangeNewKey]);
}

- (void)loadData
{
    dispatch_queue_t queue = dispatch_queue_create("loadNewData", DISPATCH_QUEUE_CONCURRENT);
    
    /// 异步执行任务
    dispatch_async(queue, ^{
        NSArray *tempArray = @[
                               @{@"name" : @"洫聙↘刀锋", @"imageURL" : @"https://www.baidu.com", @"count": @"1"},
                               @{@"name" : @"贫僧灬不虐人", @"imageURL" : @"https://www.baidu.com", @"count": @"2"},
                               @{@"name" : @"伟大旳苍穹", @"imageURL" : @"https://www.baidu.com", @"count": @"3"},
                               @{@"name" : @"绝辰冥天", @"imageURL" : @"https://www.baidu.com", @"count": @"4"},
                               @{@"name" : @"嗜魂龙吟", @"imageURL" : @"https://www.baidu.com", @"count": @"5"},
                               @{@"name" : @"乱世之巅", @"imageURL" : @"https://www.baidu.com", @"count": @"6"},
                               @{@"name" : @"不灭红神", @"imageURL" : @"https://www.baidu.com", @"count": @"7"},
                               @{@"name" : @"血战苍穹", @"imageURL" : @"https://www.baidu.com", @"count": @"8"},
                               @{@"name" : @"瞬灭天下", @"imageURL" : @"https://www.baidu.com", @"count": @"9"},
                               @{@"name" : @"血战メ傲神", @"imageURL" : @"https://www.baidu.com", @"count": @"9"},
                               @{@"name" : @"傲视判决", @"imageURL" : @"https://www.baidu.com", @"count": @"9"},
                               @{@"name" : @"一世霸主", @"imageURL" : @"https://www.baidu.com", @"count": @"9"},
                               @{@"name" : @"嗜血狱王", @"imageURL" : @"https://www.baidu.com", @"count": @"9"},
                               @{@"name" : @"孤影月下", @"imageURL" : @"https://www.baidu.com", @"count": @"9"},
                               @{@"name" : @"恨天幽狼", @"imageURL" : @"https://www.baidu.com", @"count": @"9"},
                               ];
        
        /// 首先清空数组
        [self.dataArray removeAllObjects];
        
        for (NSInteger i = 0; i < tempArray.count; i ++) {
            Model *model = [Model initModelWithDictionary:tempArray[i]];
            [self.dataArray addObject:model];
        }
        
        dispatch_async(dispatch_get_main_queue(), ^{
           /// 更新代码
            self.successBlock(self.dataArray);
        });
    });
}

- (void)dealloc
{
    [self removeObserver:self forKeyPath:@"dataArray"];
}

@end
